<?php
namespace Admin\Controller;
use Think\Controller;
class RoleController extends CommonController {
    public function roleadd(){
        //如果是post推过来的就执行保存的事件
        if(IS_POST){
            //得到POST过来的值

            $roletitle = $_POST['adminame'];
            $roles = $_POST['roles'];
            
            $str = '';
            //讲post过来的数组以逗号拼成字符串 可以插入到分组的权限表内
            $str.= implode(',', $roles);
            $sql ="insert into auth_group(title,rules) values('{$roletitle}','{$str}')";
            if(M()->execute($sql)){
                $this->success('增加成功',U('admin/admin/rolemanage'));
            }else{
                $this->error('插入有误');
            }
        }else{
            $sql = "select * from auth_rule_category ";
            //得到分类的结果
            $res = M()->query($sql);
            //遍历分类的结果 取出里面对应的cid的分类的数据
            foreach ($res as $key => $value) {
                $sql = "select * from auth_rule where cid = {$value['id']}";
                $res[$key]['rules'] = M()->query($sql);
            }
            $this->assign('res',$res);
            $this->display();
        }
    	
    }

    //角色编辑列表
    public function roleedit(){
        if(IS_POST){
            $roletitle = $_POST['adminame'];
            $roles = $_POST['roles'];
            
            $str = '';
            //讲post过来的数组以逗号拼成字符串 可以插入到分组的权限表内
            $str.= implode(',', $roles);
            $sql ="update auth_group set title = '{$roletitle}',rules = '{$str}' where id = {$_POST['groupid']}";
            if(M()->execute($sql)){
                $this->success('修改成功',U('admin/admin/rolemanage'));
            }else{
                $this->error('修改有误');
            }
        }else{
            $groupid = $_GET['groupid'];
            //首先取出当前组原有有的数据
            $old_auth_groupSql = "select * from auth_group where id = {$groupid}";  //取原有数据的Sql语句
            $old_auth_group_Result = M()->query($old_auth_groupSql);     //原有的数据
            $old_auth_group_rules = $old_auth_group_Result[0]['rules'];     //取原数据的规则rules 方便页面选中
            $rulesArr = explode(',', $old_auth_group_rules);
            $sql = "select * from auth_rule_category ";
            //得到分类的结果
            $res = M()->query($sql);
            //遍历分类的结果 取出里面对应的cid的分类的数据
            foreach ($res as $key => $value) {
                $sql = "select * from auth_rule where cid = {$value['id']}";
                $res[$key]['rules'] = M()->query($sql);
            }
            $this->assign('oldData',$old_auth_group_Result);  //分配原先的数据
            $this->assign('oldcheck',$rulesArr);
            $this->assign('res',$res);
            $this->display();  
        }
    } 

    //角色编辑删除
    public function roledelete(){
        if($_GET){
            $sql = "delete from auth_group where id = {$_GET['groupid']}";
            if(M()->execute($sql)){
                $this->success('删除成功',U('admin/admin/rolemanage'));
            }else{
                $this->error('删除有误');
            }
        }else{
            $this->error('非法访问');
        }
    }

}